<template>
  <view :style="styleObj">
    <LEchart class="h-100% w-100%" ref="chartRef" />
  </view>
</template>

<script setup>
import LEchart from '@/uni_modules/lime-echart/components/l-echart/l-echart.vue'
// #ifdef MP
const echarts = require('../../../uni_modules/lime-echart/static/echarts.min')
// #endif

const props = defineProps({
  height: {
    type: Number,
    default: 300,
  },
  data: {
    type: Array,
    default: () => {
      return []
    },
  },
  axisData: {
    type: Array,
    default: () => {
      return []
    },
  },
})

const options = computed(() => {
  return {
    grid: {
      left: '0',
      bottom: '0',
      right: '10',
      top: '0',
      containLabel: true,
    },
    yAxis: {
      type: 'category',
      data: props.axisData,
      axisLabel: {
        interval: 0,
        color: '#ffffff',
        fontSize: '14px',
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        lineStyle: {
          color: '#ffffff',
        },
      },
    },
    xAxis: {
      type: 'value',
      position: 'top',
      axisLabel: {
        interval: 0,
        color: '#9BABC8',
      },
      splitLine: {
        lineStyle: {
          color: '#9BABC8',
        },
      },
    },
    series: [
      {
        type: 'bar',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [
            { offset: 0, color: '#398294' },
            // { offset: 0.7, color: '#90e2dd' },
            { offset: 1, color: '#94FFF8' },
          ]),
          borderRadius: [0, 100, 100, 0],
        },
        barMaxWidth: 14,
        emphasis: {
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#90e2dd' },
              { offset: 0.7, color: '#90e2dd' },
              { offset: 1, color: '#4193a3' },
            ]),
          },
        },
        data: props.data,
      },
    ],
  }
})

const styleObj = computed(() => {
  return {
    height: props.height + 'px',
    width: '100%',
  }
})

const chartRef = ref(null)

const initData = async () => {
  // setTimeout(async () => {
  if (chartRef.value) {
    const myChart = await chartRef.value.init(echarts)
    myChart.setOption(options.value)
    resizeHandle()
  }
  // }, 300)
}

const resizeHandle = () => {
  chartRef.value.resize()
}

defineExpose({
  initData,
})
</script>

<style lang="scss" scoped></style>
